package com.jiedeng.config;

import com.jiedeng.util.ForwardConstants;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;

/**
 * @author 覃远龙
 * @ClassName: CrossConfig
 * @Description:
 * @date 2017/4/19 10:34
 */
@Configuration
//@EnableWebMvc //@EnableWebMvc :如果添加了该注解，则是完全控制MVC，谨慎使用
public class CrossConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    /**
     * 拦截
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(new InterceptorConfig()).addPathPatterns("/**").
                excludePathPatterns(ForwardConstants.LOGIN).excludePathPatterns("/logout").
                excludePathPatterns("/user-login").excludePathPatterns("/").
                excludePathPatterns("/checkPhoneCode").excludePathPatterns("/getVeryCode").
                excludePathPatterns("/user-resetPwd").
                excludePathPatterns("/common/*").excludePathPatterns("/passwordReset").
                excludePathPatterns("/**/*.jpg").excludePathPatterns("/**/*.png").
                excludePathPatterns("/images/*").excludePathPatterns("/**/*.css").
                excludePathPatterns("/**/*.js");
    }

    /**
     * 文件上传配置
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个数据大小
        factory.setMaxFileSize("10240KB"); // KB,MB
        /// 总上传数据大小
        factory.setMaxRequestSize("102400KB");
        return factory.createMultipartConfig();
    }
}